package com.cloud.wyf.Controller;

import com.alibaba.fastjson2.JSONObject;
import com.cloud.wyf.Entity.*;
import com.cloud.wyf.Entity.Dto.LeaderShipDispartDto;
import com.cloud.wyf.Entity.Dto.LeaderShipResultDto;
import com.cloud.wyf.Entity.Dto.MiddelDispartDto;
import com.cloud.wyf.Entity.Dto.MiddelResultDto;
import com.cloud.wyf.Service.VoteService;
import com.cloud.wyf.Util.DownloadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/download")
public class DownloadController {

    @Autowired
    VoteService voteService;

    @RequestMapping("/rate")
    public String setRate(HttpServletRequest request,HttpServletResponse response){
        String lrate = request.getParameter("lrate");
        String orate = request.getParameter("orate");
        if(lrate==null||"".equals(lrate)||orate==null||"".equals(orate))return JSONObject.toJSONString(Message.failure("date is not complete"));
        double lrates=Double.valueOf(lrate);
        double orates=Double.valueOf(orate);
        if((lrates+orates)!=1.0) return JSONObject.toJSONString(Message.failure("date is not correct"));
        request.getSession().setAttribute("lrate",lrate);
        request.getSession().setAttribute("orate",orate);
        return JSONObject.toJSONString(Message.success("setting is success"));
    }

    @RequestMapping("/leaderrate")
    public String setLeaderRate(HttpServletRequest request,HttpServletResponse response){
        String lrate = request.getParameter("lrate");
        String orate = request.getParameter("orate");
        if(lrate==null||"".equals(lrate)||orate==null||"".equals(orate))return JSONObject.toJSONString(Message.failure("date is not complete"));
        double lrates=Double.valueOf(lrate);
        double orates=Double.valueOf(orate);
        if((lrates+orates)!=1.0) return JSONObject.toJSONString(Message.failure("date is not correct"));
        request.getSession().setAttribute("leaderrate",lrate);
        request.getSession().setAttribute("odinaryrate",orate);
        return JSONObject.toJSONString(Message.success("setting is success"));
    }

    @RequestMapping("/middel")
    public void downloadMiddel(HttpServletRequest request, HttpServletResponse response){
        double lrates=0.3;
        double orates=0.7;
        Object lrate = request.getSession().getAttribute("lrate");
        Object orate = request.getSession().getAttribute("orate");
        if(lrate!=null&&!"".equals(lrate)&&orate!=null&&!"".equals(orate)){
            lrates=Double.valueOf((String) lrate);
            orates=Double.valueOf((String) orate);
        }
        List<MiddelResultDto> result=new ArrayList<>();
        List<Middel> list = voteService.queryAllMiddel(request.getHeader("x-access-token"));
        Float s=new Float(0);
        Double ss=new Double(0);
        Float os=new Float(0);
        Float os1=new Float(0);
        int quitnum=0;
        int quitnum1=0;
        for(Middel middel:list){
            List<LeaderScore> leaderScores = voteService.selectLeaderScoreByMiddelId(middel.getUid());
            List<OrdinaryScore> ordinaryScores = voteService.selectOrdinaryScoreByMid(middel.getUid());
            if(leaderScores.size()==0&&ordinaryScores.size()==0) {
                result.add(new MiddelResultDto(middel.getUid(), middel.getPart(), s, s, ss));
            }else if (leaderScores.size()!=0&&ordinaryScores.size()==0){
                MiddelResultDto middelResultDto = new MiddelResultDto(middel.getRealname(), middel.getPart(), s, s, ss);
                for(LeaderScore score:leaderScores){
                    if(score.getStatus()==1) os+=score.getScore();
                    else quitnum+=1;
                }
                if (leaderScores.size() == quitnum) middelResultDto.setLscore(os);
                else middelResultDto.setLscore(os/(leaderScores.size()-quitnum));
                middelResultDto.setOscore(s);
                middelResultDto.setTotal(middelResultDto.getLscore()*lrates);
                result.add(middelResultDto);
                quitnum=0;
                os=new Float(0);
            }else if(leaderScores.size()==0&&ordinaryScores.size()!=0){
                MiddelResultDto middelResultDto = new MiddelResultDto(middel.getRealname(), middel.getPart(), s, s, ss);
                for(OrdinaryScore score:ordinaryScores){
                    if(score.getStatus()==1) os+=score.getScore();
                    else quitnum+=1;
                }
                if (ordinaryScores.size() == quitnum) middelResultDto.setOscore(os);
                else middelResultDto.setOscore(os/(ordinaryScores.size()-quitnum));
                middelResultDto.setLscore(s);
                middelResultDto.setTotal(middelResultDto.getOscore()*orates);
                result.add(middelResultDto);
                quitnum=0;
                os=new Float(0);
            }else {
                MiddelResultDto middelResultDto = new MiddelResultDto(middel.getRealname(), middel.getPart(), s, s, ss);
                for(LeaderScore score:leaderScores){
                    if(score.getStatus()==1) os+=score.getScore();
                    else quitnum+=1;
                }

                for(OrdinaryScore score:ordinaryScores){
                    if(score.getStatus()==1) os1+=score.getScore();
                    else quitnum1+=1;
                }
                if (leaderScores.size() == quitnum) middelResultDto.setLscore(os);
                else middelResultDto.setLscore(os/(leaderScores.size()-quitnum));
                if (ordinaryScores.size() == quitnum1) middelResultDto.setOscore(os1);
                else middelResultDto.setOscore(os1/(ordinaryScores.size()-quitnum1));
                middelResultDto.setTotal(middelResultDto.getLscore()*lrates+middelResultDto.getOscore()*orates);
                result.add(middelResultDto);
                quitnum=0;
                quitnum1=0;
                os=new Float(0);
                os1=new Float(0);
            }
        }
        String[] str={"序号", "姓名", "所属组织", "领导得分","类型","普通职工得分",
                "类型","最终得分"};
        DownloadUtil.exportExcel(response,str,result);
    }

    @RequestMapping("/middelDispart")
    public void downloadMiddelDispart(HttpServletRequest request,HttpServletResponse response){
        List<Middel> list = voteService.queryAllMiddel(request.getHeader("x-access-token"));
        List<MiddelDispartDto> result=new ArrayList<>();
        int quitnum=0;
        int quitnum1=0;
        Float s=new Float(0);
        Double ss=new Double(0);
        Float os=new Float(0);
        Float os1=new Float(0);
        Float os2=new Float(0);
        Float os3=new Float(0);
        Float os4=new Float(0);
        for(Middel middel:list){
            List<LeaderScore> leaderScores = voteService.selectLeaderScoreByMiddelId(middel.getUid());
            List<OrdinaryScore> ordinaryScores = voteService.selectOrdinaryScoreByMid(middel.getUid());
            if(leaderScores.size()==0&&ordinaryScores.size()==0){
                result.add(new MiddelDispartDto(middel.getRealname(),middel.getPart(),s,s,s,s,s,ss,s,s,s,s,s,ss));
            }else if (leaderScores.size()!=0&&ordinaryScores.size()==0){
                MiddelDispartDto middelDispartDto = new MiddelDispartDto(middel.getRealname(), middel.getPart(), s,s,s,s,s,ss,s,s,s,s,s,ss);
                for(LeaderScore score:leaderScores){
                    if(score.getStatus()==1) {
                        os+=score.getItem1();
                        os1+=score.getItem2();
                        os2+=score.getItem3();
                        os3+=score.getItem4();
                        os4+=score.getItem5();
                    }
                    else quitnum+=1;
                }
                middelDispartDto.setLitem1(os/(leaderScores.size()-quitnum));
                middelDispartDto.setLitem2(os1/(leaderScores.size()-quitnum));
                middelDispartDto.setLitem3(os2/(leaderScores.size()-quitnum));
                middelDispartDto.setLitem4(os3/(leaderScores.size()-quitnum));
                middelDispartDto.setLitem5(os4/(leaderScores.size()-quitnum));
                middelDispartDto.setLtotal(new Double((os+os1+os2+os3+os4)/(5*(leaderScores.size()-quitnum))));
                result.add(middelDispartDto);
                quitnum=0;
                os=new Float(0);
                os1=new Float(0);
                os2=new Float(0);
                os3=new Float(0);
                os4=new Float(0);
            }else if(leaderScores.size()==0&&ordinaryScores.size()!=0){
                MiddelDispartDto middelDispartDto = new MiddelDispartDto(middel.getRealname(), middel.getPart(), s,s,s,s,s,ss,s,s,s,s,s,ss);
                for(OrdinaryScore score:ordinaryScores){
                    if(score.getStatus()==1) {
                        os+=score.getItem1();
                        os1+=score.getItem2();
                        os2+=score.getItem3();
                        os3+=score.getItem4();
                        os4+=score.getItem5();
                    }
                    else quitnum+=1;
                }
                middelDispartDto.setOitem1(os/(ordinaryScores.size()-quitnum));
                middelDispartDto.setOitem2(os1/(ordinaryScores.size()-quitnum));
                middelDispartDto.setOitem3(os2/(ordinaryScores.size()-quitnum));
                middelDispartDto.setOitem4(os3/(ordinaryScores.size()-quitnum));
                middelDispartDto.setOitem5(os4/(ordinaryScores.size()-quitnum));
                middelDispartDto.setOtotal(new Double((os+os1+os2+os3+os4)/(5*(ordinaryScores.size()-quitnum))));
                result.add(middelDispartDto);
                quitnum=0;
                os=new Float(0);
                os1=new Float(0);
                os2=new Float(0);
                os3=new Float(0);
                os4=new Float(0);
            }else{
                MiddelDispartDto middelDispartDto = new MiddelDispartDto(middel.getRealname(), middel.getPart(), s,s,s,s,s,ss,s,s,s,s,s,ss);
                for(LeaderScore score:leaderScores){
                    if(score.getStatus()==1) {
                        os+=score.getItem1();
                        os1+=score.getItem2();
                        os2+=score.getItem3();
                        os3+=score.getItem4();
                        os4+=score.getItem5();
                    }
                    else quitnum+=1;
                }
                middelDispartDto.setLitem1(os/(leaderScores.size()-quitnum));
                middelDispartDto.setLitem2(os1/(leaderScores.size()-quitnum));
                middelDispartDto.setLitem3(os2/(leaderScores.size()-quitnum));
                middelDispartDto.setLitem4(os3/(leaderScores.size()-quitnum));
                middelDispartDto.setLitem5(os4/(leaderScores.size()-quitnum));
                middelDispartDto.setLtotal(new Double((os+os1+os2+os3+os4)/(5*(leaderScores.size()-quitnum))));
                quitnum=0;
                os=new Float(0);
                os1=new Float(0);
                os2=new Float(0);
                os3=new Float(0);
                os4=new Float(0);
                for(OrdinaryScore score:ordinaryScores){
                    if(score.getStatus()==1) {
                        os+=score.getItem1();
                        os1+=score.getItem2();
                        os2+=score.getItem3();
                        os3+=score.getItem4();
                        os4+=score.getItem5();
                    }
                    else quitnum+=1;
                }
                middelDispartDto.setOitem1(os/(ordinaryScores.size()-quitnum));
                middelDispartDto.setOitem2(os1/(ordinaryScores.size()-quitnum));
                middelDispartDto.setOitem3(os2/(ordinaryScores.size()-quitnum));
                middelDispartDto.setOitem4(os3/(ordinaryScores.size()-quitnum));
                middelDispartDto.setOitem5(os4/(ordinaryScores.size()-quitnum));
                middelDispartDto.setOtotal(new Double((os+os1+os2+os3+os4)/(5*(ordinaryScores.size()-quitnum))));
                result.add(middelDispartDto);
                quitnum=0;
                os=new Float(0);
                os1=new Float(0);
                os2=new Float(0);
                os3=new Float(0);
                os4=new Float(0);
            }
        }
        String[] str={"序号", "姓名", "所属组织", "领导德","领导能","领导勤",
                "领导绩","领导度","总分","德","能","勤",
                "绩","度","总分"};
        DownloadUtil.exportExcel1(response,str,result);
    }

    @RequestMapping("/leaderShip")
    public void dowloadLeaderShip(HttpServletRequest request,HttpServletResponse response){
        double lrates=0.3;
        double orates=0.7;
        Object lrate = request.getSession().getAttribute("lrate");
        Object orate = request.getSession().getAttribute("orate");
        if(lrate!=null&&!"".equals(lrate)&&orate!=null&&!"".equals(orate)){
            lrates=Double.valueOf((String) lrate);
            orates=Double.valueOf((String) orate);
        }
        List<Leader> allLeader=voteService.queryAllLeader(request.getHeader("x-access-token"));
        List<Ordinary> list = voteService.queryAllOrdinary(request.getHeader("x-access-token"));
        List<LeaderShip> needLeaderShip = voteService.getNeedLeaderShip(request.getHeader("x-access-token"));
        List<LeaderShipResultDto> result=new ArrayList<>();
        int quitnum=0;
        int quitnum1=0;
        Float s=new Float(0);
        Float s1=new Float(0);
        for(LeaderShip leaderShip:needLeaderShip) {
           // for (Leader leader : allLeader) {
//                LeaderShipScore leaderShipScore = voteService.selectLeaderShipByUAndP(leader.getUid(), leaderShip.getPart());
//                if(leaderShipScore!=null) {
//                    if (leaderShipScore.getStatus() == 1) s += leaderShipScore.getScore();
//                    else quitnum++;
//                }else quitnum++;
//            }

           // }
            List<LeaderShipScore> leaderShipScores = voteService.batchSelectLeaderShipByUAndP(allLeader.stream().map(Leader::getUid).collect(Collectors.toList()), leaderShip.getPart());
            for(LeaderShipScore leaderShipScore:leaderShipScores){
                if (leaderShipScore.getStatus() == 1) s += leaderShipScore.getScore();
                else quitnum++;
            }
//            for(Ordinary ordinary:list){
//                LeaderShipScore leaderShipScore = voteService.selectLeaderShipByUAndP(ordinary.getUid(), leaderShip.getPart());
//                if(leaderShipScore!=null) {
//                    if (leaderShipScore.getStatus() == 1) s1 += leaderShipScore.getScore();
//                    else quitnum1++;
//                }else quitnum1++;
//            }
            List<LeaderShipScore> leaderShipScoress = voteService.batchSelectLeaderShipByUAndP(list.stream().map(Ordinary::getUid).collect(Collectors.toList()), leaderShip.getPart());
            for(LeaderShipScore leaderShipScore:leaderShipScoress){
                if (leaderShipScore.getStatus() == 1) s1 += leaderShipScore.getScore();
                else quitnum1++;
            }
            LeaderShipResultDto leaderShipResultDto=new LeaderShipResultDto(leaderShip.getPart(),s/(leaderShipScores.size()-quitnum),s1/(leaderShipScoress.size()-quitnum1),
                    (lrates*s/leaderShipScores.size()-quitnum)+orates*s1/(leaderShipScoress.size()-quitnum1));
            result.add(leaderShipResultDto);
            quitnum=0;
            quitnum1=0;
            s=new Float(0);
            s1=new Float(0);
        }
        String[] str={"序号", "领导班子", "领导得分","类型","普通职工得分",
                "类型","最终得分"};
        DownloadUtil.exportExcel2(response,str,result);
    }

    @RequestMapping("/leaderShipDispart")
    public void downloadLeaderShipDispart(HttpServletRequest request,HttpServletResponse response){
        List<Leader> allLeader=voteService.queryAllLeader(request.getHeader("x-access-token"));
        List<Ordinary> list = voteService.queryAllOrdinary(request.getHeader("x-access-token"));
        List<LeaderShip> needLeaderShip = voteService.getNeedLeaderShip(request.getHeader("x-access-token"));
        List<LeaderShipDispartDto> result=new ArrayList<>();
        int quitnum=0;
        int quitnum1=0;
        Float s=new Float(0);
        Float s1=new Float(0);
        Float s2=new Float(0);
        Float s3=new Float(0);
        Float s4=new Float(0);
        Float s5=new Float(0);
        Float s6=new Float(0);
        Float s7=new Float(0);
        Float s8=new Float(0);
        Float s9=new Float(0);
        for(LeaderShip leaderShip:needLeaderShip) {
            List<LeaderShipScore> leaderShipScores = voteService.batchSelectLeaderShipByUAndP(allLeader.stream().map(Leader::getUid).collect(Collectors.toList()), leaderShip.getPart());
            for(LeaderShipScore leaderShipScore:leaderShipScores){
                if (leaderShipScore.getStatus() == 1) {
                    s += leaderShipScore.getItem1();
                    s1 += leaderShipScore.getItem2();
                    s2 += leaderShipScore.getItem3();
                    s3 += leaderShipScore.getItem4();
                    s4 += leaderShipScore.getItem5();
                }
                else quitnum++;
            }
//            for (Leader leader : allLeader) {
//                LeaderShipScore leaderShipScore = voteService.selectLeaderShipByUAndP(leader.getUid(), leaderShip.getPart());
//                if(leaderShipScore!=null) {
//                    if (leaderShipScore.getStatus() == 1) {
//                        s += leaderShipScore.getItem1();
//                        s1 += leaderShipScore.getItem2();
//                        s2 += leaderShipScore.getItem3();
//                        s3 += leaderShipScore.getItem4();
//                        s4 += leaderShipScore.getItem5();
//                    } else quitnum++;
//                }else quitnum++;
//            }
            List<LeaderShipScore> leaderShipScoress = voteService.batchSelectLeaderShipByUAndP(list.stream().map(Ordinary::getUid).collect(Collectors.toList()), leaderShip.getPart());
            for(LeaderShipScore leaderShipScore:leaderShipScoress){
                if (leaderShipScore.getStatus() == 1) {
                    s5 += leaderShipScore.getItem1();
                    s6 += leaderShipScore.getItem2();
                    s7 += leaderShipScore.getItem3();
                    s8 += leaderShipScore.getItem4();
                    s9 += leaderShipScore.getItem5();
                }
                else quitnum1++;
            }
//            for(Ordinary ordinary:list){
//                LeaderShipScore leaderShipScore = voteService.selectLeaderShipByUAndP(ordinary.getUid(), leaderShip.getPart());
//                if(leaderShipScore!=null) {
//                    if (leaderShipScore.getStatus() == 1) {
//                        s5 += leaderShipScore.getItem1();
//                        s6 += leaderShipScore.getItem2();
//                        s7 += leaderShipScore.getItem3();
//                        s8 += leaderShipScore.getItem4();
//                        s9 += leaderShipScore.getItem5();
//                    } else quitnum1++;
//                }else quitnum1++;
//            }
            LeaderShipDispartDto leaderShipDispartDto=new LeaderShipDispartDto(leaderShip.getPart(),s/(leaderShipScores.size()-quitnum),s1/(leaderShipScores.size()-quitnum),
                    s2/(leaderShipScores.size()-quitnum),s3/(leaderShipScores.size()-quitnum),s4/(leaderShipScores.size()-quitnum),new Double((s/(leaderShipScores.size()-quitnum)+s1/(leaderShipScores.size()-quitnum)+
                    s2/(leaderShipScores.size()-quitnum)+s3/(leaderShipScores.size()-quitnum)+s4/(leaderShipScores.size()-quitnum))/5),s5/(leaderShipScoress.size()-quitnum1),s6/(leaderShipScoress.size()-quitnum1),
                    s7/(leaderShipScoress.size()-quitnum1),s8/(leaderShipScoress.size()-quitnum1),s9/(leaderShipScoress.size()-quitnum1),new Double((s5/(leaderShipScoress.size()-quitnum1)+s6/(leaderShipScoress.size()-quitnum1)+
                    s7/(leaderShipScoress.size()-quitnum1)+s8/(leaderShipScoress.size()-quitnum1)+s9/(leaderShipScoress.size()-quitnum1))/5));
            result.add(leaderShipDispartDto);
            quitnum=0;
            quitnum1=0;
            s=new Float(0);
            s1=new Float(0);
            s2=new Float(0);
            s3=new Float(0);
            s4=new Float(0);
            s5=new Float(0);
            s6=new Float(0);
            s7=new Float(0);
            s8=new Float(0);
            s9=new Float(0);
        }
        String[] str={"序号", "领导班子", "领导德","领导能","领导勤",
                "领导绩","领导度","总分","德","能","勤",
                "绩","度","总分"};
        DownloadUtil.exportExcel3(response,str,result);
    }
}
